# coding:utf-8
import json
from typing import Dict

from PyQt6.QtWidgets import QWidget
from qfluentwidgets import ToolTipFilter

from views.template_page_view import BasePageTemplateView

from .common.translator import Translator
from common.utils.utils_log import AntLogger


class DemoChatView(BasePageTemplateView):
    """示例图表界面"""

    def __init__(self, parent=None):
        # 使用对象名初始化基类
        super().__init__(objectName="demoChatView", parent=parent)

        # 初始化UI
        self.init_ui()

    def init_ui(self):
        """初始化UI"""
        t = Translator()

        # 添加图表模板
        card_result = self.addEchatTemplate(
            title=self.tr("图表案例"),
            chatName="base_chat_demo",
            dataFun=self.process_chat_data,
            datParamFun=self.get_chart_params
        )

    def process_chat_data(self, json_data: dict, params: Dict[str, str]) -> str:
        """
        处理图表数据

        参数:
        ----------
        json_data: dict
            JSON配置数据
        params: Dict[str, str]
            参数字典

        返回:
        ----------
        str: 处理后的JSON字符串
        """
        # 准备图表数据
        source_data = [
            ["月份", "M-5", "M-4", "M-3", "M-2", "M-1", "M", "M+1"],
            ["国盛金控", 56.5, 82.1, 88.7, 70.1, 53.4, 85.1, 41.2],
            ["风华高科", 51.1, 51.4, 55.1, 53.3, 73.8, 68.7, 41.2],
            ["海信家电", 40.1, 62.2, 69.5, 36.4, 45.2, 32.5, 41.2],
            ["永辉超市", 25.2, 37.1, 41.2, 18, 33.9, 49.1, 41.2],
        ]

        # 更新数据源
        json_data["dataset"]["source"] = source_data

        # 转换为JSON字符串
        return json.dumps(json_data, ensure_ascii=False)

    def get_chart_params(self) -> Dict[str, str]:
        """
        获取图表参数

        返回:
        ----------
        Dict[str, str]: 参数字典
        """
        # 这里可以返回任何需要的参数
        return {}

    def showEvent(self, event):
        """显示事件处理"""
        super().showEvent(event)

        # 显示成功信息
        if not self.loaded:
            self.addInfoBar(
                info_type="success",
                title="成功",
                message="图表加载完成",
                duration=2000
            )
            self.loaded = True
